package com.sgb.zhaocai.widget;

import android.content.Context;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;

import com.sgb.link.view.adapter.CommonCategoryFilterPopAdapter;
import com.sgb.zhaocai.R;
import com.swgk.core.customview.MaxRecyclerView;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.util.PickSelectEntity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MemberTypePopWindow extends BasePopWindow {
    //层级title
    private String[][] titles = new String[][]{
            new String[]{"", "二级分类", "三级分类", "四级分类", "五级分类"},

    };
    private TextView resetStep;
    private TextView sureStep;
    private LinearLayout llOneCategory;
    private TextView tvOneType;
    private MaxRecyclerView typeOneList;
    private LinearLayout llTwoCategory;
    private TextView tvTwoType;
    private MaxRecyclerView typeTwoList;
    private LinearLayout llThreeCategory;
    private TextView tvThreeType;
    private MaxRecyclerView typeThreeList;
    private LinearLayout llFourCategory;
    private TextView tvFourType;
    private MaxRecyclerView typeFourList;
    private LinearLayout llFiveCategory;
    private TextView tvFiveType;
    private MaxRecyclerView typeFiveList;
    //    private boolean isAttention;
    private CommonCategoryFilterPopAdapter oneAdapter;
    private CommonCategoryFilterPopAdapter twoAdapter;
    private CommonCategoryFilterPopAdapter threeAdapter;
    private CommonCategoryFilterPopAdapter fourAdapter;
    private CommonCategoryFilterPopAdapter fiveAdapter;

    private int onePos;
    private int twoPos;
    private int threePos;
    private int fourPos;
    private int fivePos;
    private PickSelectEntity oneName = new PickSelectEntity("", "全部");
    private PickSelectEntity twoName = new PickSelectEntity();
    private PickSelectEntity threeName = new PickSelectEntity();
    private PickSelectEntity fourName = new PickSelectEntity();
    private PickSelectEntity fiveName = new PickSelectEntity();
    private List<PickSelectEntity<List<PickSelectEntity<List<PickSelectEntity<List<PickSelectEntity<List<PickSelectEntity>>>>>>>>> dataList = new ArrayList<>();
    private List<PickSelectEntity<List<PickSelectEntity<List<PickSelectEntity<List<PickSelectEntity>>>>>>> twoList = new ArrayList<>();
    private List<PickSelectEntity<List<PickSelectEntity<List<PickSelectEntity>>>>> threeList = new ArrayList<>();
    private List<PickSelectEntity<List<PickSelectEntity>>> fourList = new ArrayList<>();
    private List<PickSelectEntity> fiveList = new ArrayList<>();


    public MemberTypePopWindow(Context context) {
        super(context);
        initData();
    }

   /* public MemberTypePopWindow(Context context, boolean isAttention) {
        super(context);
        this.isAttention = isAttention;
        initData();
    }*/


    @Override
    protected int popLayout() {
        return R.layout.pop_member_type;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        //重置
        resetStep = holder.getPop().findViewById(R.id.reset_step);
        //确认
        sureStep = holder.getPop().findViewById(R.id.sure_step);
        //一级
        llOneCategory = holder.getPop().findViewById(R.id.ll_one_category);
        tvOneType = holder.getPop().findViewById(R.id.tv_one_type);
        typeOneList = holder.getPop().findViewById(R.id.type_one_list);
        //二级
        llTwoCategory = holder.getPop().findViewById(R.id.ll_two_category);
        tvTwoType = holder.getPop().findViewById(R.id.tv_two_type);
        typeTwoList = holder.getPop().findViewById(R.id.type_two_list);
        //三级
        llThreeCategory = holder.getPop().findViewById(R.id.ll_three_category);
        tvThreeType = holder.getPop().findViewById(R.id.tv_three_type);
        typeThreeList = holder.getPop().findViewById(R.id.type_three_list);
        //四级
        llFourCategory = holder.getPop().findViewById(R.id.ll_four_category);
        tvFourType = holder.getPop().findViewById(R.id.tv_four_type);
        typeFourList = holder.getPop().findViewById(R.id.type_four_list);
        //五级
        llFiveCategory = holder.getPop().findViewById(R.id.ll_five_category);
        tvFiveType = holder.getPop().findViewById(R.id.tv_five_type);
        typeFiveList = holder.getPop().findViewById(R.id.type_five_list);
        tvOneType.setText("一级分类");
        tvTwoType.setText("二级分类");
        tvThreeType.setText("三级分类");
        tvFourType.setText("四级分类");
        tvFiveType.setText("五级分类");
    }

    private void initData() {
        oneDataView();
        twoDataView();
        threeDataView();
        fourDataView();
        fiveDataView();
        //重置
        resetStep.setOnClickListener(view -> {
            onePos = 0;
            //重置二、三、四、五级数据
            OneResetData();
            oneAdapter.setItemSelect(true);
            oneAdapter.setDefItem(0);
            /*if (mCategoryCallBack != null) {
                mCategoryCallBack.resetPosition();
            }*/
            oneName = new PickSelectEntity("", "全部");
        });
        //确定
        sureStep.setOnClickListener(view -> {
            if (mCategoryCallBack != null) {
                mCategoryCallBack.resultPosition(oneName, twoName, threeName, fourName, fiveName, onePos, twoPos, threePos, fourPos, fivePos);
            }
            dissMiss();
        });
    }

    //一级数据信息
    private void oneDataView() {
        oneAdapter = new CommonCategoryFilterPopAdapter(mContext, null);
        typeOneList.setLayoutManager(new GridLayoutManager(mContext, 4));
        typeOneList.setHasFixedSize(false);
        typeOneList.setAdapter(oneAdapter);
        oneAdapter.setItemSelect(true);
        oneAdapter.setDefItem(0);
        oneAdapter.setOnItemClick(new AdapterOnItemClick<PickSelectEntity<List<PickSelectEntity<List<PickSelectEntity<List<PickSelectEntity<List<PickSelectEntity>>>>>>>>>() {
            @Override
            public void onItemClick(PickSelectEntity<List<PickSelectEntity<List<PickSelectEntity<List<PickSelectEntity<List<PickSelectEntity>>>>>>>> onePickSelectEntity, int position) {
                twoList.clear();
                oneName = onePickSelectEntity;
                onePos = position;
                //重置二级、三级、四级、五级数据
                OneResetData();
                if (onePickSelectEntity.getChildren() != null && onePickSelectEntity.getChildren().size() > 0) {
                    llTwoCategory.setVisibility(View.VISIBLE);
                    twoList.add(new PickSelectEntity("", "全部"));
                    twoList.addAll(onePickSelectEntity.getChildren());
                    twoAdapter.setDatas(twoList);
                    twoAdapter.setDefItem(0);
                }
            }
        });
    }

    //二级数据
    private void twoDataView() {
        twoAdapter = new CommonCategoryFilterPopAdapter(mContext, null);
        typeTwoList.setLayoutManager(new GridLayoutManager(mContext, 4));
        typeTwoList.setHasFixedSize(false);
        typeTwoList.setAdapter(twoAdapter);
        twoAdapter.setItemSelect(true);
        twoAdapter.setDefItem(0);
        twoAdapter.setOnItemClick(new AdapterOnItemClick<PickSelectEntity<List<PickSelectEntity<List<PickSelectEntity<List<PickSelectEntity>>>>>>>() {
            @Override
            public void onItemClick(PickSelectEntity<List<PickSelectEntity<List<PickSelectEntity<List<PickSelectEntity>>>>>> twoPickSelectEntity, int position) {
                threeList.clear();
                twoName = twoPickSelectEntity;
                twoPos = position;
                //重置三级、四级、五级数据
                TwoResetData();
                if (twoPickSelectEntity.getChildren() != null && twoPickSelectEntity.getChildren().size() > 0) {
                    llThreeCategory.setVisibility(View.VISIBLE);
                    threeList.add(new PickSelectEntity("", "全部"));
                    threeList.addAll(twoPickSelectEntity.getChildren());
                    threeAdapter.setDatas(threeList);
                    threeAdapter.setDefItem(0);
                }
            }
        });
    }


    //三级数据
    private void threeDataView() {
        threeAdapter = new CommonCategoryFilterPopAdapter(mContext, null);
        typeThreeList.setLayoutManager(new GridLayoutManager(mContext, 4));
        typeThreeList.setHasFixedSize(false);
        typeThreeList.setAdapter(threeAdapter);
        threeAdapter.setItemSelect(true);
        threeAdapter.setDefItem(0);
        threeAdapter.setOnItemClick(new AdapterOnItemClick<PickSelectEntity<List<PickSelectEntity<List<PickSelectEntity>>>>>() {
            @Override
            public void onItemClick(PickSelectEntity<List<PickSelectEntity<List<PickSelectEntity>>>> threePickSelectEntity, int position) {
                fourList.clear();
                threeName = threePickSelectEntity;
                threePos = position;
                //重置四级、五级数据
                ThreeResetData();
                if (threePickSelectEntity.getChildren() != null && threePickSelectEntity.getChildren().size() > 0) {
                    llFourCategory.setVisibility(View.VISIBLE);
                    fourList.add(new PickSelectEntity("", "全部"));
                    fourList.addAll(threePickSelectEntity.getChildren());
                    fourAdapter.setDatas(fourList);
                    fourAdapter.setDefItem(0);
                }
            }
        });
    }

    //四级数据
    private void fourDataView() {
        fourAdapter = new CommonCategoryFilterPopAdapter(mContext, null);
        typeFourList.setLayoutManager(new GridLayoutManager(mContext, 4));
        typeFourList.setHasFixedSize(false);
        typeFourList.setAdapter(fourAdapter);
        fourAdapter.setItemSelect(true);
        fourAdapter.setDefItem(0);
        fourAdapter.setOnItemClick(new AdapterOnItemClick<PickSelectEntity<List<PickSelectEntity>>>() {
            @Override
            public void onItemClick(PickSelectEntity<List<PickSelectEntity>> fourPickSelectEntity, int position) {
                fiveList.clear();
                fourName = fourPickSelectEntity;
                fourPos = position;
                //重置五级数据
                FourResetData();
                if (fourPickSelectEntity.getChildren() != null && fourPickSelectEntity.getChildren().size() > 0) {
                    llFiveCategory.setVisibility(View.VISIBLE);
                    fiveList.add(new PickSelectEntity("", "全部"));
                    fiveList.addAll(fourPickSelectEntity.getChildren());
                    fiveAdapter.setDatas(fiveList);
                    fiveAdapter.setDefItem(0);
                }
            }
        });
    }

    //五级数据
    private void fiveDataView() {
        fiveAdapter = new CommonCategoryFilterPopAdapter(mContext, null);
        typeFiveList.setLayoutManager(new GridLayoutManager(mContext, 4));
        typeFiveList.setHasFixedSize(false);
        typeFiveList.setAdapter(fiveAdapter);
        fiveAdapter.setItemSelect(true);
        fiveAdapter.setDefItem(0);
        fiveAdapter.setOnItemClick(new AdapterOnItemClick<PickSelectEntity>() {
            @Override
            public void onItemClick(PickSelectEntity fivePickSelectEntity, int position) {
                fiveName = fivePickSelectEntity;
                fivePos = position;
            }
        });
    }

    @Override
    public void dismiss() {
        super.dismiss();
        if (onDisMiss != null)
            onDisMiss.disMiss();
    }

    /**
     * 设置数据
     *
     * @param memberTypeList
     */
    public void setData(List<PickSelectEntity<List<PickSelectEntity<List<PickSelectEntity<List<PickSelectEntity<List<PickSelectEntity>>>>>>>>> memberTypeList) {
        dataList.clear();
        if (memberTypeList != null && memberTypeList.size() > 0) {
            llOneCategory.setVisibility(View.VISIBLE);
            dataList.add(new PickSelectEntity("", "全部"));
            dataList.addAll(memberTypeList);
            oneAdapter.setDatas(dataList);
        }
    }

    /**
     * 回显数据信息
     */
    public void setEchoData(int onePos, int twoPos, int threePos, int fourPos, int fivePos) {
        this.onePos = onePos;
        this.twoPos = twoPos;
        this.threePos = threePos;
        this.fourPos = fourPos;
        this.fivePos = fivePos;
        if (onePos > -1) {
            oneAdapter.setItemSelect(true);
            oneAdapter.setDefItem(onePos);
        }
        if (twoPos > -1) {
            twoAdapter.setItemSelect(true);
            twoAdapter.setDefItem(twoPos);
        }
        if (threePos > -1) {
            threeAdapter.setItemSelect(true);
            threeAdapter.setDefItem(threePos);
        }
        if (fourPos > -1) {
            fourAdapter.setItemSelect(true);
            fourAdapter.setDefItem(fourPos);
        }
        if (fivePos > -1) {
            fiveAdapter.setItemSelect(true);
            fiveAdapter.setDefItem(fivePos);
        }
    }

    //重置二级、三级、四级、五级数据
    private void OneResetData() {
        llTwoCategory.setVisibility(View.GONE);
        twoAdapter.clearDatas();
        twoPos = -1;
        twoName = new PickSelectEntity("", "");
        llThreeCategory.setVisibility(View.GONE);
        threeAdapter.clearDatas();
        threeName = new PickSelectEntity("", "");
        threePos = -1;
        llFourCategory.setVisibility(View.GONE);
        fourAdapter.clearDatas();
        fourName = new PickSelectEntity("", "");
        fourPos = -1;
        llFiveCategory.setVisibility(View.GONE);
        fiveAdapter.clearDatas();
        fiveName = new PickSelectEntity("", "");
        fivePos = -1;
    }

    //重置三级、四级、五级数据
    private void TwoResetData() {
        llThreeCategory.setVisibility(View.GONE);
        threeAdapter.clearDatas();
        threeName = new PickSelectEntity("", "");
        threePos = -1;
        llFourCategory.setVisibility(View.GONE);
        fourAdapter.clearDatas();
        fourName = new PickSelectEntity("", "");
        fourPos = -1;
        llFiveCategory.setVisibility(View.GONE);
        fiveAdapter.clearDatas();
        fiveName = new PickSelectEntity("", "");
        fivePos = -1;
    }

    //重置四级、五级数据
    private void ThreeResetData() {
        llFourCategory.setVisibility(View.GONE);
        fourAdapter.clearDatas();
        fourName = new PickSelectEntity("", "");
        fourPos = -1;
        llFiveCategory.setVisibility(View.GONE);
        fiveAdapter.clearDatas();
        fiveName = new PickSelectEntity("", "");
        fivePos = -1;
    }

    //重置五级数据
    private void FourResetData() {
        llFiveCategory.setVisibility(View.GONE);
        fiveAdapter.resetCheckDatas();
        fiveName = new PickSelectEntity("", "");
        fivePos = -1;
    }

    private CategoryCallBack mCategoryCallBack;

    public void setCategoryCallBack(CategoryCallBack categoryCallBack) {
        mCategoryCallBack = categoryCallBack;
    }

    public interface CategoryCallBack {
        //结果位置
        void resultPosition(PickSelectEntity oneName, PickSelectEntity twoName, PickSelectEntity threeName, PickSelectEntity fourName, PickSelectEntity fiveName,
                            int onePos, int twoPos, int threePos, int fourPos, int fivePos);

        //重置pos
        void resetPosition();
    }
}
